#ifndef _THREAD_ENUMERATOR_H_
#define _THREAD_ENUMERATOR_H_

#include "tools/HandleWraper.h"

#include "Thread.h"


#include <vector>

namespace sd
{
namespace tools
{


/*
	On windows system load process threads is VERY slowly operation
*/

namespace thread_enumerator
{
namespace impl
{
	class ProcessThreadEnumeratorImpl;
};



class ProcessThreadEnumerator
{
	typedef impl::ProcessThreadEnumeratorImpl Impl;
	typedef std::vector<int>::const_iterator inner_iterator;
	inner_iterator it;
public:

	class iterator
	{
	public:
		iterator( inner_iterator it ): it_(it), thread_(0){}
		~iterator(){ delete thread_; }

		iterator& operator++(){ dropObj(); ++it_; return *this; }
		iterator& operator--(){ dropObj(); --it_; return *this; }
		thread::Thread& operator->(){ checkObj(); return *thread_;  }
		thread::Thread& operator*(){ checkObj(); return *thread_;  }

		int getId()const{ return *it_; }

		bool operator==( const iterator& ref )const{ return it_ == ref.it_; }
		bool operator!=( const iterator& ref )const{ return it_ != ref.it_; }
	private:
		void dropObj(){ delete thread_; thread_=0; }
		void checkObj(){ if(!thread_)thread_=new thread::Thread(*it_); }

		thread::Thread* thread_;
		inner_iterator it_;
	};

	ProcessThreadEnumerator();
	ProcessThreadEnumerator( int process );
	ProcessThreadEnumerator( const HHANDLE& handle );
	~ProcessThreadEnumerator();

	void reload();

	iterator begin()const;
	iterator end()const;
private:
	Impl* impl_;
};





}//thread_enumerator
}//tools
}//sd









#endif //THREAD_ENUMERATOR_H_